unit EditPlayers;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, ExtCtrls, StdCtrls, Oracle, uSQLs, ComCtrls, gnugettext, strutils,
  dblookup, DB, OracleData, DBCtrls, umainutils;

type
  TuFrmEditPlayers = class(TForm)
    edt_Name: TLabeledEdit;
    edt_Surname: TLabeledEdit;
    edt_Street: TLabeledEdit;
    pnl_Buttons: TPanel;
    btn_Save: TSpeedButton;
    btn_Close: TSpeedButton;
    OracleQuery1: TOracleQuery;
    edt_City: TLabeledEdit;
    dtp_Birth: TDateTimePicker;
    Label1: TLabel;
    edt_Nickname: TLabeledEdit;
    edt_EMail: TLabeledEdit;
    pnl_YearsNotOk: TPanel;
    chb_CanMail: TCheckBox;
    Label2: TLabel;
    img_Country: TImage;
    OracleDataSetPlayer: TOracleDataSet;
    com_Country: TDBLookupComboBox;
    DataSource1: TDataSource;
    procedure btn_SaveClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btn_CloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure dtp_BirthChange(Sender: TObject);
    procedure edt_NameChange(Sender: TObject);
    procedure edt_SurnameKeyPress(Sender: TObject; var Key: Char);
    procedure com_CountryClick(Sender: TObject);
  private
    { Private declarations }
    m_recordID: string;
    m_IsClone, m_IsEdit: boolean;
    procedure PopulateCountryCombo();
  public
    NewRecordId: string;
    IsTypingInEdit: boolean;
    procedure ClearBoxes();
    procedure ShowForm(IsEdit, IsClone: boolean; recordID: string; TicketWindow: boolean);
    { Public declarations }
  end;

var
  uFrmEditPlayers: TuFrmEditPlayers;

implementation

uses UfrmMain, ufraplayers, uUtils, ufrmsellticket;
{$R *.dfm}
{ TuFrmEditPlayers }

procedure TuFrmEditPlayers.btn_CloseClick(Sender: TObject);
begin
  close();
end;

procedure TuFrmEditPlayers.btn_SaveClick(Sender: TObject);
var
  Birth_Date: string;
begin
  Birth_Date := FormatDateTime('dd.mm.yyyy', dtp_Birth.Date);
  Birth_Date := 'to_date(''' + Birth_Date + ''', ''dd.mm.yyyy'')';
  if m_IsClone or (not m_IsEdit) then
  begin
    NewRecordId := InsertDataIntoDB('PLAYERS', 'CAN_MAIL,EMAIL,NAME,SURNAME,BIRTH_DATE,STREET,CITY,COUNTRY_ID,NICKNAME', '''' + BToStr(chb_CanMail.Checked)
        + ''', ''' + edt_EMail.Text + ''', ''' + edt_Name.Text + ''', ''' + edt_Surname.Text + ''', ' + Birth_Date + ', ''' + edt_Street.Text + ''', ''' + edt_City.Text + ''', ''' + VARtostr
        (com_Country.KeyValue) + ''', ''' + edt_Nickname.Text + '''');
  end
  else
  begin
    UpdateDataToDB('PLAYERS', 'CAN_MAIL=''' + BToStr(chb_CanMail.Checked) + '''' + ',EMAIL=''' + edt_EMail.Text + '''' + ', NAME = ''' + edt_Name.Text + ''', NICKNAME = ''' + edt_Nickname.Text +
        ''', SURNAME = ''' + edt_Surname.Text + ''',      Birth_Date = ' + Birth_Date + ',STREET=''' + edt_Street.Text + '''' + ',CITY=''' + edt_City.Text + '''' + ',COUNTRY_ID=''' + VARtostr
        (com_Country.KeyValue) + '''', 'ID=' + m_recordID);
  end;
  ClearBoxes();
  if self.BorderStyle <> bsNone then
  begin
    FrmMain.GetGridData(FrmMain.FraPlayersMain.stg_Players, SQL_Players, false);
    FrmMain.FraPlayersMain.stg_PlayersSelectCell(self, 0, FrmMain.FraPlayersMain.stg_Players.Row, canselect);
    close();
  end
  else
  begin
    ufrmsellticket.frm_SellTicket.PopulatePlayerCombo('', '', nil);
    ufrmsellticket.frm_SellTicket.com_Player.KeyValue := NewRecordId;
    ufrmsellticket.frm_SellTicket.com_Player2Click(self);
  end;
end;

procedure TuFrmEditPlayers.FormCreate(Sender: TObject);
begin
  gnugettext.TranslateComponent(self);
end;

procedure TuFrmEditPlayers.PopulateCountryCombo();
begin
  // ---
  if FrmMain.OracleSession1.Connected then
  begin
    OracleDataSetPlayer.CloseAll;
    OracleDataSetPlayer.Session := FrmMain.OracleSession1;
    OracleDataSetPlayer.SQL.Clear;
    OracleDataSetPlayer.SQL.Text := ('select id, name from countries order by name asc');
    // ---
    OracleDataSetPlayer.Open;
    DataSource1.DataSet := OracleDataSetPlayer;
    com_Country.ListSource := DataSource1;
    com_Country.KeyField := 'ID';
    com_Country.ListField := 'NAME';
  end;
end;

procedure TuFrmEditPlayers.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    close
  else if Key = #13 then
    btn_Save.Click;
end;

procedure TuFrmEditPlayers.FormShow(Sender: TObject);
begin
  edt_Name.SetFocus;
  PopulateCountryCombo();
end;

procedure TuFrmEditPlayers.ClearBoxes();
begin
  img_Country.Picture := nil;
  edt_Name.Text := '';
  edt_Surname.Text := '';
  edt_Nickname.Text := '';
  dtp_Birth.Date := now() - (18 * 365);
  dtp_BirthChange(self);
  edt_Street.Text := '';
  edt_City.Text := '';
  com_Country.KeyValue := null;
  edt_EMail.Text := '';
  chb_CanMail.Checked := false;
  m_IsEdit := false;
  m_IsClone := false;
end;

procedure TuFrmEditPlayers.com_CountryClick(Sender: TObject);
begin
  img_Country.Picture := uUtils.GetImage('SELECT IMAGE, IMAGE_TYPE FROM COUNTRIES WHERE ID=' + IntToStr(com_Country.KeyValue), FrmMain.OracleSession1)
end;

procedure TuFrmEditPlayers.dtp_BirthChange(Sender: TObject);
begin
  if (((now) - dtp_Birth.Date + 1) / 365) < 18 then
  begin
    pnl_YearsNotOk.Visible := true;
  end
  else
  begin
    pnl_YearsNotOk.Visible := false;
  end;
end;

procedure TuFrmEditPlayers.edt_NameChange(Sender: TObject);
var
  OldChange: TNotifyEvent;
  OldStart: Integer;
begin
  if IsTypingInEdit then
  begin
    with (Sender as TLabeledEdit) do
    begin
      OldChange := OnChange;
      OnChange := nil;
      OldStart := SelStart;
      Text := AnsiUpperCase(LeftStr(Text, 1)) + AnsiLowerCase(MidStr(Text, 2, Length(Text)));
      OnChange := OldChange;
      SelStart := OldStart;
    end;
    if ((TLabeledEdit(Sender) = edt_Name) or (TLabeledEdit(Sender) = edt_Surname)) and ((self.BorderStyle = bsNone) and (ufrmsellticket.frm_SellTicket.showing)) then
    begin
      ufrmsellticket.frm_SellTicket.com_Player.Visible := true;
      ufrmsellticket.frm_SellTicket.PopulatePlayerCombo(edt_Name.Text, edt_Surname.Text, self);
      ufrmsellticket.frm_SellTicket.pnl_SeatTablePos.Visible := true;
//      ufrmsellticket.frm_SellTicket.pnl_TicketType.Caption := 'BUY';
  //    ufrmsellticket.frm_SellTicket.m_TicketType := 'BUY';
    end;
  end;
end;

procedure TuFrmEditPlayers.edt_SurnameKeyPress(Sender: TObject; var Key: Char);
begin
  IsTypingInEdit := true;
end;

procedure TuFrmEditPlayers.ShowForm(IsEdit, IsClone: boolean; recordID: string; TicketWindow: boolean);
var
  Query: string;
begin
  ClearBoxes();
  m_IsClone := IsClone;
  m_recordID := recordID;
  m_IsEdit := IsEdit;
  OracleQuery1.Session := FrmMain.OracleSession1;
  if IsEdit then
  begin
    Query := 'select id, EMAIL, can_mail, name,NICKNAME, surname, birth_date, street, city, country_ID from players where id=' + recordID;
    OracleQuery1.SQL.Clear;
    OracleQuery1.SQL.Text := (Query);
    OracleQuery1.Execute;
    if not OracleQuery1.Eof then
    begin
      edt_Name.Text := OracleQuery1.Field('NAME');
      edt_EMail.Text := OracleQuery1.Field('EMAIL');
      edt_Surname.Text := OracleQuery1.Field('surname');
      edt_Nickname.Text := OracleQuery1.Field('NICKNAME');
      dtp_Birth.Date := OracleQuery1.Field('birth_date');
      edt_Street.Text := OracleQuery1.Field('street');
      edt_City.Text := OracleQuery1.Field('city');
      com_Country.KeyValue := OracleQuery1.Field('country_ID');
      com_CountryClick(self);
      chb_CanMail.Checked := StrToB(OracleQuery1.Field('can_mail'));
    end;
  end;
  dtp_BirthChange(self);
  if not TicketWindow then
  begin
    ShowModal();
  end;
end;

end.
